# 3.2 Action

# 1. Definition of Action

Public methods defined within a Controller are known as Actions. An Action represents the smallest unit of a request. Action methods must be defined inside a Controller and should be publicly visible.

public class HelloController extends Controller {
    public void index() {
       renderText("This method is an action");
    }
    public String test() {
       return "index.html";
    }
}
1
2
3
4
5
6
7
8

In the above code, two Actions are defined: HelloController.index() and HelloController.test().

Actions can have return values. These return values can be obtained in interceptors using invocation.getReturnValue(), which can be useful for controlling rendering.

# 2. @NotAction Annotation

If you want a public method in a controller not to act as an action, you can use the @NotAction annotation. The @NotAction annotation is often used for intermediate Controllers that inherit from a BaseController, for example:

public class BaseController extends Controller {
   
   // Doesn't wish to be an action, only for child class or interceptor calls
   @NotAction
   public void getLoginUser() {
   
   }
}
1
2
3
4
5
6
7
8

# 3. Routing Mapping for Controller Superclass

Starting from JFinal 3.6, by default, all methods in the controller superclass won't be mapped as actions. (Meaning, in the JFinal 3.6 version, the @NotAction in the above BaseController example is no longer necessary, as BaseController is the superclass of your final controller, XxxController).

If you want the methods in the superclass to be mapped as actions, just add the following configuration:

public void configRoute(Routes me) {
    me.setMappingSuperClass(true);
}
1
2
3

This feature is for performance optimization. Projects with a large number of routes can speed up the startup. If this configuration is set in the "child Routes", it will only be effective for that "child Routes", for example:

public FrontRoutes extends Routes {
   public void config() {
      // The configuration here is only effective for routes under FrontRoutes. It is recommended to configure this way to improve performance.
      setMappingSuperClass(true);
      
      add("/weixin", WeixinController.class);
   }
}
1
2
3
4
5
6
7
8

More details about "child Routes" can be found in the second chapter, the configRoute section.

Last Updated: 9/17/2023, 5:50:29 AM